package com.imdroid.carac.activities;

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

@SuppressWarnings("deprecation")
public class OptionsSeekBarPreference extends DialogPreference {
	/**
	 * The context of the view
	 */
	private Context context;
	/**
	 * The text view of caption
	 */
	private TextView textView = null;
	/**
	 * The seek bar
	 */
	private SeekBar seekBar = null;
	/**
	 * The linear layout
	 */
	private LinearLayout layout = null;
	/**
	 * The max value
	 */
	private int maxValue = 0;
	/**
	 * The max value
	 */
	private int minValue = 0;
	/**
	 * The unit
	 */
	private String unit = null;

	public OptionsSeekBarPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
	}

	/* (non-Javadoc)
	 * @see android.preference.DialogPreference#onPrepareDialogBuilder(android.app.AlertDialog.Builder)
	 */
	protected void onPrepareDialogBuilder(Builder builder) {
		// 添加布局
		layout = new LinearLayout(context);
		layout.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT)); // 布局属性

		layout.setMinimumWidth(400); // 布局的最小宽度
		layout.setPadding(20, 20, 20, 20); // 上下左右的Padding

		// 添加SeekBar
		seekBar = new SeekBar(context);
		seekBar.setMax(maxValue - minValue);

		textView = new TextView(context);

		seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			/*
			 * (non-Javadoc)
			 * 
			 * @see
			 * android.widget.SeekBar.OnSeekBarChangeListener#onProgressChanged
			 * (android.widget.SeekBar, int, boolean)
			 */
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				int value = seekBar.getProgress() + minValue;
				textView.setText(value + (unit != null ? unit : ""));
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see android.widget.SeekBar.OnSeekBarChangeListener#
			 * onStartTrackingTouch (android.widget.SeekBar)
			 */
			public void onStartTrackingTouch(SeekBar seekBar) {

			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see android.widget.SeekBar.OnSeekBarChangeListener#
			 * onStopTrackingTouch (android.widget.SeekBar)
			 */
			public void onStopTrackingTouch(SeekBar seekBar) {

			}

		});
		seekBar.setLayoutParams(new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT)); // SeekBar的布局属性
		seekBar.setProgress(getPersistedInt(0) - minValue); // 设置默认值

		int value = seekBar.getProgress() + minValue;
		textView.setText(value + (unit != null ? unit : ""));

		layout.addView(textView);
		layout.addView(seekBar); // 把SeekBar加到 layout的布局中

		builder.setView(layout);
	}

	protected void onDialogClosed(boolean positiveResult) {
		if (positiveResult) {
			persistInt(seekBar.getProgress() + minValue); // 保存SeekBar的值
		}
		super.onDialogClosed(positiveResult);
	}

	/**
	 * @param minValue
	 *            the minValue to set
	 * @param maxValue
	 *            the maxValue to set
	 * @param unit
	 *            the unit to set
	 */
	public void setRange(int minValue, int maxValue, String unit) {
		this.minValue = minValue;
		this.maxValue = maxValue;
		this.unit = unit;
	}

	/**
	 * @return the maxValue
	 */
	public int getMaxValue() {
		return maxValue;
	}

	/**
	 * @param maxValue
	 *            the maxValue to set
	 */
	public void setMaxValue(int maxValue) {
		this.maxValue = maxValue;
	}

	/**
	 * @return the minValue
	 */
	public int getMinValue() {
		return minValue;
	}

	/**
	 * @param minValue
	 *            the minValue to set
	 */
	public void setMinValue(int minValue) {
		this.minValue = minValue;
	}

	/**
	 * @return the unit
	 */
	public String getUnit() {
		return unit;
	}

	/**
	 * @param unit
	 *            the unit to set
	 */
	public void setUnit(String unit) {
		this.unit = unit;
	}
}